package com.makenv.web.backend;

import com.makenv.domain.Video;
import com.makenv.service.VideoService;
import com.makenv.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/manage/video")
public class VideoManageController {

    @Autowired
    VideoService videoService;

    // 每页显示的行数
    private Integer row;

    @RequestMapping("/add")
    public String add(@RequestParam String name, @RequestParam String author, @RequestParam String description,
                      @RequestParam String link, @RequestParam String category) {

        videoService.insert(name, author, description, link, category);
        return "redirect:/manage/video/showback";
    }

    @RequestMapping("/jumpback")
    public String jumpback(@RequestParam int pageNum, HttpServletRequest request) {
        int pageSum = getPageSum();
        request.getSession().setAttribute("pageSum", pageSum);
        request.getSession().setAttribute("pageNum", pageNum);
        List<Video> videoList = videoService.showVideoList((pageNum - 1) * row, row, null);
        request.setAttribute("videoList", videoList);
        return "/back/resource";
    }

    /**
     * @Author : lilimin
     * @Description : 显示后台页面的书籍
     * @Date : Created in 14:47 2017/12/4
     */

    @RequestMapping("/showback")
    public String showBack(HttpServletRequest request) {

        forShow(request);
        return "back/resource";
    }

    @RequestMapping("/update")
    public String update(@RequestParam int id, @RequestParam String name, @RequestParam String author,
                         @RequestParam String description, @RequestParam String link, @RequestParam String category) {

        videoService.updateByPrimaryKey(id ,name, author, description, link, category);
        return "redirect:/manage/video/showback";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam String jsonStr) {
        String[] idArray = jsonStr.split(",");
        for (String str : idArray) {
            int i = Integer.valueOf(str);
            videoService.deleteByPrimaryKey(i);
        }

        //这个请求是由ajax发送上来的，SpringMVC不会进行页面的跳转，用ajax进行跳转
        return "";
    }

    public void forShow(HttpServletRequest request) {
        row = Config.getInt("showRow");
        List<Video> videoList = videoService.showVideoList(0, row, null);
        int pageSum = getPageSum();
        request.getSession().setAttribute("pageSum", pageSum);
        request.getSession().setAttribute("pageNum", 1);
        request.setAttribute("videoList", videoList);
    }

    public Integer getPageSum() {
        int pageSum;
        int totalBook = videoService.getVideoSum();
        if (totalBook % row == 0) {
            pageSum = totalBook / row;
        } else {
            pageSum = totalBook / row + 1;
        }
        return pageSum;
    }

}
